package com.baiwei.vpnapi.controller;
import com.baiwei.vpnapi.entity.reqentity.BuyPackageInput;
import com.baiwei.vpnapi.entity.reqentity.VpnconfigInput;
import com.baiwei.vpnapi.service.impl.VpnServiceImpl;
import com.baiwei.vpnapi.utils.JwtTokenUtil;
import com.baiwei.vpnapi.utils.ResultVoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

/**
 * @program: vpnapi
 * @description: vpn相关操作
 * @author: Mr. Stone
 * @create: 2020-02-24 16:29
 **/

@RestController
@RequestMapping("/vpn")

public class VpnController {
    @Autowired
    private VpnServiceImpl vpnService;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    // 套餐列表
    @GetMapping("/packages")
    public Object packageList() {
        return ResultVoUtils.success(vpnService.getAllDiscountPackage());
        //return ResultVoUtils.success(vpnService.getPackages());
    }


    // 线路列表
    @GetMapping("/servers")
    public Object serverList() {
        return ResultVoUtils.success(vpnService.getOrderedServers());
    }

    // 线路测试
    @GetMapping("/test")
    public Object testServers(){
        return ResultVoUtils.success(vpnService.getServers());
    }

    // 连接
    @PostMapping("/connect")
    public Object connectVpn(@RequestBody VpnconfigInput input) {
        input.validation();
        return ResultVoUtils.success(vpnService.connect(input.getServerId()));
    }

    // 断开连接
    @PostMapping("/disconnect")
    public Object disconnectVpn() {
        return ResultVoUtils.success(vpnService.disconnect());
    }

    // 购买套餐
    @PostMapping("/buy")
    public Object buyPackage(@RequestBody BuyPackageInput info, HttpServletRequest req) {
        String token = req.getHeader("Authorization");
        String userIdString = jwtTokenUtil.getUsernameFromToken(token);
        info.setUserId(userIdString);
        return ResultVoUtils.success(vpnService.buyPackage(info));
    }

    //用户最近的有效套餐，包括在使用的和未使用的
    @GetMapping("/lastPackage")
    public Object lastPackage(HttpServletRequest req) {
        String token = req.getHeader("Authorization");
        String userIdString = jwtTokenUtil.getUsernameFromToken(token);
        return ResultVoUtils.success(vpnService.getLastPackage(userIdString));
    }

    // 用户当前套餐
    @GetMapping("/currentPackage")
    public Object currentPackage(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String userIdString = jwtTokenUtil.getUsernameFromToken(token);
        return ResultVoUtils.success(vpnService.getCurrentPackage(userIdString));
    }

    // 用户购买套餐记录
    @GetMapping("/historyPackage")
    public Object historyPackage(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String userIdString = jwtTokenUtil.getUsernameFromToken(token);
        return ResultVoUtils.success(vpnService.getHistoryPackage(userIdString));
    }
}
